package com.qss.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qs.mapper.SysOperatorMapper;
import com.qs.pojo.SysOperator;
import com.qs.pojo.SysOperatorExample;
import com.qss.service.OperatorService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-10-08 14:29
 * @packageName com.qss.service.impl
 */
@Service(interfaceClass = OperatorService.class,timeout = 10000)
public class OperatorServiceImpl implements OperatorService {
   @Autowired
   private SysOperatorMapper mapper;
    @Override
    public boolean saveLogger(SysOperator logger) {
        return mapper.insertSelective(logger)==1;
    }

    @Override
    public PageInfo<SysOperator> queryUsers(SysOperator user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysOperatorExample ex = new SysOperatorExample();
        SysOperatorExample.Criteria criteria = ex.createCriteria();
        if(user!=null){
            if (user.getModuleName()!=null){
                criteria.andModuleNameLike("%"+user.getModuleName()+"%");
            }
        }
        List<SysOperator> users = mapper.selectByExample(ex);
        return new PageInfo(users);
    }

    @Override
    public SysOperator queryUserById(Integer operId) {
        return mapper.selectByPrimaryKey(operId);
    }

    @Override
    public boolean saveUser(SysOperator user) {
        if(user.getOperId()!=null){
            int flag =mapper.updateByPrimaryKeySelective(user);
            return flag==1?true:false;
        }
        int flag = mapper.insertSelective(user);
        return flag==1?true:false;
    }

    @Override
    public boolean delUserById(Integer[] ids) {
        boolean status = false;
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public SysOperator queryUserByUserName(String moduleName) {
        SysOperatorExample ex = new SysOperatorExample();
        SysOperatorExample.Criteria criteria = ex.createCriteria();
        criteria.andModuleNameEqualTo(moduleName);
        List<SysOperator> users = mapper.selectByExample(ex);
        if (users==null ||users.size()==0){
            return null;

        }
        return users.get(0);
    }
}
